package com.easy.user.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.DeliveryAddr;
import com.easy.common.service.IDeliveryAddrService;
import com.easy.user.mapper.DeliveryAddrMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@DubboService
public class DeliveryAddrServiceImpl extends ServiceImpl<DeliveryAddrMapper, DeliveryAddr> implements IDeliveryAddrService {

    @Override
    public DeliveryAddr getAddrById(int id) {
        return super.getOne(new QueryWrapper<DeliveryAddr>().eq("id", id));
    }

    @Override
    public List<DeliveryAddr> getAddrsByUserId(int userId) {
        LambdaQueryWrapper<DeliveryAddr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliveryAddr::getUser_id, userId);
        queryWrapper.eq(DeliveryAddr::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public int addAddr(DeliveryAddr deliveryAddr) {
        // 确保删除状态默认为0(未删除)
        if (deliveryAddr.getIs_del() == null) {
            deliveryAddr.setIs_del(0);
        }
        return super.save(deliveryAddr) ? 1 : 0;
    }

    @Override
    public int updateAddr(DeliveryAddr deliveryAddr) {
        deliveryAddr.setUpdate_time(LocalDateTime.now());
        return super.updateById(deliveryAddr) ? 1 : 0;
    }

    @Override
    public int deleteAddr(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

//    @Override
//    public int setDefaultAddr(Integer id, Integer userId) {
//        // 先将该用户的所有地址设置为非默认
//        super.update().set("is_default", 0).eq("user_id", userId).update();
//        // 再将指定地址设置为默认
//        return super.update().set("is_default", 1).eq("id", id).update() ? 1 : 0;
//    }

    @Override
    public List<DeliveryAddr> getAll(DeliveryAddr deliveryAddr) {
        LambdaQueryWrapper<DeliveryAddr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliveryAddr::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<DeliveryAddr> getPage(DeliveryAddr deliveryAddr, IPage<DeliveryAddr> page) {
        LambdaQueryWrapper<DeliveryAddr> queryWrapper = new LambdaQueryWrapper<>();
        if (deliveryAddr.getId() != null) {
            queryWrapper.eq(DeliveryAddr::getId, deliveryAddr.getId());
        }
        if (deliveryAddr.getUser_id() != null) {
            queryWrapper.eq(DeliveryAddr::getUser_id, deliveryAddr.getUser_id());
        }
        if (deliveryAddr.getReceiver_name() != null){
            queryWrapper.like(DeliveryAddr::getReceiver_name, deliveryAddr.getReceiver_name());
        }
        if (deliveryAddr.getReceiver_phone() != null){
            queryWrapper.like(DeliveryAddr::getReceiver_phone, deliveryAddr.getReceiver_phone());
        }
        if (deliveryAddr.getIs_default() != null){
            queryWrapper.eq(DeliveryAddr::getIs_default, deliveryAddr.getIs_default());
        }
        queryWrapper.eq(DeliveryAddr::getIs_del, 0);
        return super.page(page, queryWrapper);
    }
}
